//
//  GiveupSelectWindow.m
//  CloudTest
//
//  Created by 张帅 on 2020/7/30.
//  Copyright © 2020 张帅. All rights reserved.
//

#import "GiveupSelectWindow.h"
@interface GiveupSelectWindow ()
@property(nonatomic,strong)UIView * baseView;
@property(nonatomic,assign)CGFloat oldHeight;
@property(nonatomic,strong)UIView * bottomView;
@property(nonatomic,strong)UIButton * btn2;
@property(nonatomic,strong)SelectedView * sView;
@end
@implementation GiveupSelectWindow

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = RGBA(0, 0, 0, 0.45);
        [self uiSet];
    }
    return self;
}
- (void)uiSet {
    self.baseView = [[UIView alloc] initWithFrame:CGRectMake(27, 0, self.width-27-27, 100)];
    self.baseView.layer.cornerRadius =12;
    self.baseView.layer.masksToBounds= YES;
    self.baseView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.baseView];
    
    
    UILabel * titleLabel = [UIFactory create_LabelContent:@"放弃并退款" textColor:nil fontSize:17];
    titleLabel.textAlignment  = NSTextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:17];
    [self.baseView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(16);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(self.baseView.mas_right).offset(-20);
        make.height.mas_equalTo(24);
    }];

    NSString * content = @"因考级未开始，本次放弃订单可以同时进行退款，点击“放弃并退款”，审核通过后，款项将于1-7个工作日内原路退回到您的支付账户。";
    
    UILabel * contentLabel = [UIFactory create_LabelContent:content textColor:RGBA(0, 0, 0, 0.45) fontSize:14];
    contentLabel.numberOfLines= 0;
    [self.baseView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(titleLabel.mas_bottom).offset(16);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(self.baseView.mas_right).offset(-20);
    }];
    
    [self.baseView layoutIfNeeded];
    
    self.sView = [[SelectedView alloc] initWithFrame:CGRectMake(16, contentLabel.bottom+16, self.baseView.width - 32, 44)];
    @weakify(self);
    [self.sView setClick_block:^(int type) {
         @strongify(self);
        if (self.sView.selected_content.length != 0) {
            self.btn2.enabled = YES;
        } else {
            self.btn2.enabled = NO;
        }
        [UIView animateWithDuration:0.5 animations:^{
             if (type == 0) {
               self.baseView.height = self.oldHeight;
             } else {
                 self.baseView.height = self.sView.bottom+16+50;
             }
            self.bottomView.top = self.sView.bottom + 16;
            self.baseView.centerY = self.centerY;
        }];
    }];
    [self.baseView addSubview:self.sView];
    
    
    self.bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, self.sView.bottom+16, self.baseView.width, 50)];
    [self.baseView addSubview:self.bottomView];
   
    //
       UIView *line1 = [[UIView alloc] initWithFrame:CGRectZero];
        line1.backgroundColor = color(@"#F0F0F0");
        [self.bottomView addSubview:line1];
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.mas_equalTo(0);
            make.height.mas_equalTo(0.5);
        }];
//    //
    UIButton * btn = [UIFactory create_btnWithTitle:@"取消" imageName:nil backgroundColor:nil TypefaceColor:RGBA(0, 0, 0, 0.45) fontSize:16 target:self action:@selector(btnClicked:)];
        [self.bottomView addSubview:btn];
        btn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(line1.mas_bottom);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(self.bottomView.mas_centerX);
            make.height.mas_equalTo(50);
        }];

    self.btn2 = [UIFactory create_btnWithTitle:@"放弃并退款" imageName:nil backgroundColor:nil TypefaceColor:theme_color fontSize:16 target:self action:@selector(btnClicked:)];
    self.btn2.enabled = NO;
        [self.bottomView addSubview:self.btn2];
        self.btn2.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [self.btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(line1.mas_bottom);
            make.right.mas_equalTo(self.bottomView.mas_right);
            make.left.mas_equalTo(self.bottomView.mas_centerX);
            make.height.mas_equalTo(50);
        }];

        UIView * line2 = [[UIView alloc] initWithFrame:CGRectZero];
          line2.backgroundColor = color(@"#F0F0F0");
          [self.bottomView addSubview:line2];
          [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
              make.top.mas_equalTo(line1.mas_bottom);
              make.centerX.mas_equalTo(self.bottomView);
              make.width.mas_equalTo(0.5);
              make.bottom.mas_equalTo(self.btn2.mas_bottom);
          }];
    
    self.baseView.height = self.bottomView.bottom;
    self.baseView.centerY = self.centerY;
    self.oldHeight = self.baseView.height;
    
}

- (void)dismis {
    [self removeFromSuperview];
}
- (void)btnClicked:(UIButton *)btn {
     [self dismis];
    if ([btn.titleLabel.text isEqualToString:@"放弃并退款"]) {
         if (self.clicked_block) {
               self.clicked_block(self.sView.selected_content);
           }
    }
  
}


/// 显示放弃弹框
/// @param block block
+ (void)show_clicked_block:(void(^)(NSString * content))block {
    UIWindow * window = [UIApplication sharedApplication].keyWindow;
    GiveupSelectWindow * selfView = [[GiveupSelectWindow alloc] initWithFrame:window.bounds];
    selfView.clicked_block = block;
    [window addSubview:selfView];
}

@end

#pragma mark ----------------------        SelectedView       ----------------------

@interface SelectedView ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSArray * dataArray;
@property(nonatomic,strong)UITextField * textF;
@property(nonatomic,strong)UIImageView * imageView;
@property(nonatomic,strong)UITableView * tableView;
@property(nonatomic,assign)NSInteger selected_row; //选中行
@end
@implementation SelectedView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self uiSet];
    }
    return self;
}
- (void)uiSet {
    self.selected_row = -1;
    
    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 6;
    self.layer.masksToBounds = YES;
    self.layer.borderColor = [RGBA(0, 0, 0, 0.15) CGColor];
    self.layer.borderWidth = 1;
    
   self.imageView = [UIFactory create_ImageView_ImageName:@"向下箭头"];
    self.imageView.highlightedImage= UIImageMake(@"向上箭头");
    self.imageView.frame = CGRectMake(self.width-24-10, self.height/2-12, 24, 24);
//    self.imageView.
    [self addSubview:self.imageView];
    
    
    self.textF = [[UITextField alloc] initWithFrame:CGRectMake(14, 0, self.width-self.imageView.width-10, 44)];
    self.textF.font = [UIFont systemFontOfSize:14];
    self.textF.placeholder = @"请选择放弃理由";
    self.textF.userInteractionEnabled = NO;
    [self addSubview:self.textF];
    
    UIButton * select_btn = [UIFactory create_btnWithTitle:@"" imageName:nil backgroundColor:nil TypefaceColor:theme_color fontSize:14 target:self action:@selector(btnClicked:)];
    select_btn.frame = self.bounds;
    [self addSubview:select_btn];
    
//    self.dataArray = @[@"信息填写有误",@"考级项目选择有误",@"需要再准备一下",@"不想考了",@"其他",];
    [self addSubview:self.tableView];
    
    
    [GiveupModel get_give_up_message_success:^(id obj) {
        self.dataArray = obj;
        self.tableView.height = self.dataArray.count * 44;
        [self.tableView reloadData];
    } error:^{
        
    }];
    
}

- (UITableView *)tableView {
    if (_tableView==nil) {
        CGFloat height = self.dataArray.count * 44;
        _tableView = [[QMUITableView alloc] initWithFrame:CGRectMake(0, self.height, self.width, height) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
       _tableView.dataSource = self;
        _tableView.hidden = YES;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
#pragma mark - <QMUITableViewDataSource, QMUITableViewDelegate>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return  self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return CGFLOAT_MIN;
}
#pragma mark UITableViewCell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"UITableViewCell";
     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
     if (!cell) {
         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
         cell.selectionStyle=UITableViewCellSelectionStyleNone;
     }
    GiveupModel *model =  self.dataArray[indexPath.row];
    cell.textLabel.text  = model.dictLabel;
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    if (self.selected_row == indexPath.row) {
        cell.textLabel.textColor = theme_color;
        UIImageView * seleImageview = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 16, 11)];
         seleImageview.image = UIImageMake(@"选择对号");
        cell.accessoryView = seleImageview;
    } else {
        cell.textLabel.textColor = RGBA(0, 0, 0, 0.65);
        cell.accessoryView = nil;
    }
     return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    self.selected_row = indexPath.row;
    GiveupModel *model =  self.dataArray[indexPath.row];
    self.textF.text = model.dictLabel;
    self.selected_content = model.dictValue;
    [tableView reloadData];
    [self btnClicked:nil];
}
- (void)btnClicked:(UIButton *)btn {
    int i = 0;
    if (self.imageView.highlighted) {
        i=0;
        [UIView animateWithDuration:0.5 animations:^{
            self.height = 44;
        } completion:^(BOOL finished) {
            self.tableView.hidden = YES;
        }];
    } else {
        i=1;
        [UIView animateWithDuration:0.5 animations:^{
            self.height = self.tableView.height + self.textF.height;
            self.tableView.hidden = NO;
        }];
    }
    self.imageView.highlighted = !self.imageView.highlighted;
    if (self.click_block) {
        self.click_block(i);
    }
}
@end


@implementation GiveupModel
//获取放弃理由列表
+ (void)get_give_up_message_success:(ObjSuccess)success error:(ErrorBlock)errorBlock {
    NSString *url =[NSString stringWithFormat:@"%@give_up_message",get_dic_type_url];
    [NetWorkTools requestWithURLStr:url method:GET params:nil success:^(id responseObject) {
            NSLog(@"获取放弃理由列表 %@",responseObject);
            if ([AppTools isStateSuccess:responseObject]) {
                NSArray * list = [NSArray yy_modelArrayWithClass:[GiveupModel class] json:responseObject[@"data"]];
                if (success) {
                    success(list);
                }
            } else {
                if (errorBlock) {
                    errorBlock();
                }
            }
        } failure:^(NSError *error) {
            NSLog(@"获取放弃理由列表error %@",error);
            NSString *errorStr = [self errorDescribe:error];
            [QMUITips showError:errorStr];
        }];
}


@end

